from typing import Annotated

from fastapi import APIRouter, Depends, status

from src.api.controllers.responses.auth import UserWithToken
from src.api.providers import Stub
from src.api.providers.auth import AuthProvider
from src.core.exceptions.user import UserIdNotExist, UserTgIdNotExist
from src.core.models import dto
from src.core.models.dto import UserDTO
from src.database.services import UserService

user_router = APIRouter(
    prefix="/users",
    tags=["users"],
)


@user_router.get(
    "/",
    responses={
        status.HTTP_200_OK: {"model": dto.UserDTO},
        status.HTTP_404_NOT_FOUND: {"model": UserTgIdNotExist},
    },
)
async def get_user(
    auth_provider: Annotated[AuthProvider, Depends(Stub(AuthProvider))],
    user: Annotated[UserDTO, Depends(Stub(UserDTO))],
) -> UserWithToken:
    token = auth_provider.create_user_token(user.phone)
    return UserWithToken(user, token)


@user_router.post(
    "/{user_id}",
    responses={
        status.HTTP_200_OK: {"model": dto.UserDTO},
        status.HTTP_400_BAD_REQUEST: {"model": UserIdNotExist},
    },
)
async def update_user(
    user_id: int,
    new_user: UserDTO,
    user_service: Annotated[UserService, Depends(Stub(UserService))],
) -> UserDTO:
    user = await user_service.update_user(
        user_id,
        name=new_user.name,
        telegram_id=new_user.telegram_id,
        phone=new_user.phone,
        email=new_user.email,
        onboarding=True,
    )
    return user
